import pygame
import sys
import g
from character import Ken
import net
from util import *
import content

class Game:
	def __init__(self, zoom = 2):
		pygame.display.init()
		pygame.font.init()

		self.width = 384
		self.height = 288
		self.stageWidth = 700
		self.zoom = zoom

		self._screen = pygame.display.set_mode((self.width * self.zoom, self.height * self.zoom))
		self.screen = pygame.surface.Surface((self.width, self.height))
		
		self.scroll = (0, 0)

		self.clock = pygame.time.Clock()

		self.chars = []
		self.objs = []
		self.objsDel = []
		self.debugText = []
		#self.textDel = []
		self.charFrames = {}
		
		self.frameNum = 0

		self.paused = False
		self.drawHitboxes = False

	def createObject(self, name, *args):
		pass

	def killObject(self, obj):
		self.objsDel.append(obj)

	def initialize(self):
		pass

	def preGameRun(self):
		pass
		
	def netUpdate(self):
		pass

	def handleKeyDown(self, key):
		pass
		
	def showDebugText(self, text, x, y):
		textObj = {"text":text, "x":x, "y":y, "life": 1.0}
		self.debugText.append(textObj)
		print(textObj)

	def run(self):
		roundOver = False
		gameOver = False
		self.frameNum = 0

		elapsed = 1.0 / 60.0
		debugFont = pygame.font.SysFont(None, 20)

		self.preGameRun()

		while not roundOver:
			self.netUpdate()

			self.clock.tick(60)


			for evt in pygame.event.get():
				if evt.type == pygame.QUIT:
					gameOver = True
					roundOver = True

				if evt.type == pygame.KEYDOWN:
					self.handleKeyDown(evt.key)

					if evt.key == pygame.K_SLASH:
						self.drawHitboxes = not self.drawHitboxes

			sx = (self.chars[0].x + self.chars[1].x) / 2 - self.width / 2
			sx = min(max(sx, 0), self.stageWidth - self.width)
			sy = 0
			self.scroll = (sx, sy)

			self.chars[0].x = min(max(self.chars[0].x, 0), self.stageWidth)
			if self.chars[0].x <= self.scroll[0] or self.chars[0].x >= self.scroll[0] + self.width:
				self.chars[0].cannotMoveBack = True
			else:
				self.chars[0].cannotMoveBack = False

			self.chars[1].x = min(max(self.chars[1].x, 0), self.stageWidth)
			if self.chars[1].x <= self.scroll[0] or self.chars[1].x >= self.scroll[0] + self.width:
				self.chars[1].cannotMoveBack = True
			else:
				self.chars[1].cannotMoveBack = False

			self.screen.fill((255,255,255))
			self.screen.blit(content.images["backgrounds"]["bg1.png"], (-self.scroll[0], -self.scroll[1] - 244))

			for o in self.objs:
				o.render(self.screen, self.scroll)
				if self.paused or self.drawHitboxes:
					o.renderHitbox(self.screen, self.scroll)

			for t in [dt for dt in self.debugText if dt["life"] > 0]:
				t["life"] -= elapsed
				t["y"] -= 1
				debugSurf = debugFont.render(t["text"], False, (0,255,255))
				debugSurf.set_alpha(t["life"] * 255)
				self.screen.blit(debugSurf, (t["x"] - debugSurf.get_width()/2 - self.scroll[0], t["y"] - self.scroll[1]))

			# Health bars
			hpw = (self.chars[0].health / self.chars[0].maxHealth)
			hpw = max(hpw, 0)
			hpc = ((1-hpw) * 255, hpw * 255, 0)
			hpw *= 180
			pygame.draw.rect(self.screen, hpc, (5, 5, hpw, 10), 0)

			hpw = (self.chars[1].health / self.chars[1].maxHealth)
			hpw = max(hpw, 0)
			hpc = ((1-hpw) * 255, hpw * 255, 0)
			hpw *= 180
			pygame.draw.rect(self.screen, hpc, (371, 5, -hpw, 10), 0)


			pygame.transform.scale(self.screen, (self.width * self.zoom, self.height * self.zoom), self._screen)

			pygame.display.update()

			if self.chars[0].x < self.chars[1].x:
				self.chars[0].setFacing("RIGHT")
				self.chars[1].setFacing("LEFT")
			else:
				self.chars[0].setFacing("LEFT")
				self.chars[1].setFacing("RIGHT")

			if not self.paused:
				for o in self.objs:
					o.update(self.frameNum % 4 == 0)
				self.frameNum += 1

			solveCollision(self.objs)
			for obj in self.objsDel:
				try:
					self.objs.remove(obj)
				except(ValueError):
					pass
			self.objsDel = []
